package com.clstu.qqserver.service;

import com.clstu.qqcommon.Message;
import com.clstu.qqcommon.MessageType;
import com.clstu.utility.IOUtility;

import java.util.HashMap;
import java.util.Set;

/**
 * 这是一个服务器推送新闻的类
 */
public class SentNewsToAllClient implements Runnable{

    @Override
    public void run() {
        //表示新闻内容
        String news = "";
        while (!news.equals("-1")){//输入-1退出该线程
            System.out.print("请输入要推送的消息/新闻(输入-1退出):");
            news = IOUtility.readString();
            Message message = new Message("服务器", null, news, null, MessageType.MESSAGE_SENT_ALL);
            HashMap<String, ServerConnectClientThread> hm = ServerConnectClientThreadManager.getUserConnectionThreadMap();
            Set<String> keys = hm.keySet();
            for (String key:keys) {
                hm.get(key).sentMessage(message);
            }
        }
    }
}